# -*- coding: utf-8 -*-
import scrapy
from xicidaili.items import XicidailiItem


class DbsgwSpider(scrapy.Spider):
    name = 'dbsgw'
    allowed_domains = ['dbsgw.cn']
    start_urls = ['https://blog.dbsgw.cn/page/4']

    def parse(self, response):
        dbsgw_content = response.xpath('/html/body/section/div[1]/div/article')
        for o in dbsgw_content:
            item = XicidailiItem()
            item['name'] = o.xpath('./p[2]/time/text()').get()
            item['url'] = o.xpath('./header/h2/a/@href').get()
            # print(item)
            # yield item  # 抛出异常
            yield scrapy.Request(item['url'], callback=self.tiqu, meta={'item': item})

        dbsgw_url3 = response.xpath('/html/body/section/div[1]/div/div/ul/li[8]/a/@href').get()
        print(dbsgw_url3)
        yield scrapy.Request(dbsgw_url3, callback=self.parse)  # 转给自身函数

    def tiqu(self, response):
        item = response.meta['item']
        # print(response.body)
        item['content'] = response.xpath('/html/body/section/div[2]/div/article/text()').getall()
        # print(item)
        yield item
